package com.ruoyi.common.core.utils;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.aliyun.dypnsapi20170525.Client;
import com.aliyun.dypnsapi20170525.models.GetFusionAuthTokenRequest;
import com.aliyun.dypnsapi20170525.models.GetFusionAuthTokenResponse;
import com.aliyun.dypnsapi20170525.models.VerifyWithFusionAuthTokenRequest;
import com.aliyun.dypnsapi20170525.models.VerifyWithFusionAuthTokenResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.ruoyi.common.core.web.domain.dto.pack.AliFusionDto;
import com.ruoyi.common.core.domain.vo.AliPhoneVo;

/**
 * @author: Wuzh
 * @date: 2025-03-12 10:58
 * @description: 阿里云融合通信工具
 */
public class AliPhoneUtil {

    /**
     * <b>description</b> :
     * <p>使用AK&amp;SK初始化账号Client</p>
     * @return Client
     * @throws Exception
     */
    public static Client createClient () throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考。
        // 建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html。
        Config config = new Config ()
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId ( "LTAI5tFZXNax3zUfthwSNDSc" )
                // 必填，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret ( "YQPIhzC8UUPpYhMv1tuons1iTgtNs5" );
        // Endpoint 请参考 https://api.aliyun.com/product/Dypnsapi
        config.endpoint = "dypnsapi.aliyuncs.com";
        return new Client ( config );
    }


    public static AliPhoneVo getToken (AliFusionDto aliPhoneVerifyTokenReq) throws Exception {
        Client client = createClient ();
        // 构造请求参数
        GetFusionAuthTokenRequest request = new GetFusionAuthTokenRequest ()
                .setSchemeCode ( aliPhoneVerifyTokenReq.getSchemeCode() )
                .setPlatform ( aliPhoneVerifyTokenReq.getPlatform() ) // 或 "iOS"
                .setBundleId ( aliPhoneVerifyTokenReq.getBundleId() ).
                        setDurationSeconds ( aliPhoneVerifyTokenReq.getDurationSeconds() );
//        GetFusionAuthTokenRequest request = new GetFusionAuthTokenRequest ()
//                .setSchemeCode ( "FA000000007000122304" )
//                .setPlatform ( "iOS" ) // 或 "iOS"
//                .setBundleId ( "com.taichoo.lehuoquan" ).
//                        setDurationSeconds ( 3600L );

        // 调用接口
        GetFusionAuthTokenResponse response = client.getFusionAuthToken ( request );
        // 打印返回结果
        System.out.println ( JSON.toJSONString ( response.getBody () ) );
        AliPhoneVo aliPhoneBo = JSONUtil.toBean ( JSON.toJSONString ( response.getBody () ), AliPhoneVo.class );
        return aliPhoneBo;
    }


    public static AliPhoneVo verifyToken (AliFusionDto aliPhoneVerifyTokenReq) throws Exception {
        //2.通过Token获取手机号
        Client client = createClient ();
        VerifyWithFusionAuthTokenRequest verifyWithFusionAuthTokenRequest = new VerifyWithFusionAuthTokenRequest ()
                .setVerifyToken ( aliPhoneVerifyTokenReq.getVerifyToken () );
        RuntimeOptions runtime = new RuntimeOptions ();
        try {
            // 复制代码运行请自行打印 API 的返回值
            VerifyWithFusionAuthTokenResponse verifyWithFusionAuthTokenResponse = client.verifyWithFusionAuthTokenWithOptions ( verifyWithFusionAuthTokenRequest, runtime );
            AliPhoneVo aliPhoneBo = JSONUtil.toBean ( JSON.toJSONString ( verifyWithFusionAuthTokenResponse.getBody () ), AliPhoneVo.class );
            return aliPhoneBo;
        } catch ( TeaException error ) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println ( error.getMessage () );
            // 诊断地址
            System.out.println ( error.getData ().get ( "Recommend" ) );
            com.aliyun.teautil.Common.assertAsString ( error.message );
        } catch ( Exception _error ) {
            TeaException error = new TeaException ( _error.getMessage (), _error );
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println ( error.getMessage () );
            // 诊断地址
            System.out.println ( error.getData ().get ( "Recommend" ) );
            com.aliyun.teautil.Common.assertAsString ( error.message );
        }
        return null;
    }

}
